package org.dromara.system.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.excel.core.DefaultExcelListener;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.system.domain.bo.WbItemInfoBo;
import org.dromara.system.domain.dr.WbMinuteBidBagDR;
import org.dromara.system.domain.vo.WbItemInfoVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.WbProjectInfoVo;
import org.dromara.system.domain.bo.WbProjectInfoBo;
import org.dromara.system.service.IWbProjectInfoService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 计划
 *
 * @author Lion Li
 * @date 2024-04-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/projectInfo")
public class WbProjectInfoController extends BaseController {

    private final IWbProjectInfoService wbProjectInfoService;

    /**
     * 查询计划列表
     */
    @SaCheckPermission("system:projectInfo:list")
    @GetMapping("/list")
    public TableDataInfo<WbProjectInfoVo> list(WbProjectInfoBo bo, PageQuery pageQuery) {
        return wbProjectInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出计划列表
     */
    @SaCheckPermission("system:projectInfo:export")
    @Log(title = "计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WbProjectInfoBo bo, HttpServletResponse response) {
        List<WbProjectInfoVo> list = wbProjectInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "计划", WbProjectInfoVo.class, response);
    }

    /**
     * 导出模板计划列表
     */
    @SaCheckPermission("system:projectInfo:export")
    @Log(title = "计划", businessType = BusinessType.EXPORT)
    @PostMapping("/exportTemplate")
    public void exportTemplate(HttpServletResponse response) {
        List<WbProjectInfoVo> list = new ArrayList<>();
        WbProjectInfoVo wbProjectInfoVo = new WbProjectInfoVo();
        wbProjectInfoVo.setItemId("182401");
        wbProjectInfoVo.setItemName("国网江西省电力有限公司2024年第一次服务招标采购（220kV）");
        wbProjectInfoVo.setEstimatePublishTime(new Date());
        wbProjectInfoVo.setProvinceName("江西省");
        list.add(wbProjectInfoVo);
        ExcelUtil.exportExcel(list, "计划", WbProjectInfoVo.class, response);
    }

    /**
     * 导入计划表
     */
    @SaCheckPermission("system:projectInfo:import")
    @Log(title = "计划", businessType = BusinessType.IMPORT)
    @PostMapping("/importDate")
    public R importDate(@RequestParam("file") MultipartFile file) throws IOException {
        ExcelResult<WbProjectInfoVo> result = ExcelUtil.importExcel(file.getInputStream(), WbProjectInfoVo.class, new DefaultExcelListener(true));
        return wbProjectInfoService.importDate(result);
    }

    /**
     * 获取计划详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:projectInfo:query")
    @GetMapping("/{id}")
    public R<WbProjectInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(wbProjectInfoService.queryById(id));
    }

    /**
     * 新增计划
     */
    @SaCheckPermission("system:projectInfo:add")
    @Log(title = "计划", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WbProjectInfoBo bo) {
        bo.setStatus("-1");//默认状态为（-1：草稿）
        return toAjax(wbProjectInfoService.insertByBo(bo));
    }

    /**
     * 修改计划
     */
    @SaCheckPermission("system:projectInfo:edit")
    @Log(title = "计划", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WbProjectInfoBo bo) {
        return toAjax(wbProjectInfoService.updateByBo(bo));
    }

    /**
     * 批量发布项目计划
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:itemInfo:edit")
    @Log(title = "计划", businessType = BusinessType.UPDATE)
    @PutMapping("/release/{ids}")
    public R<Void> release(@NotEmpty(message = "主键不能为空")
                           @PathVariable Long[] ids) {
        for (Long id : ids) {
            WbProjectInfoVo wbProjectInfoVo = wbProjectInfoService.queryById(id);
            if("-1".equals(wbProjectInfoVo.getStatus())){
                wbProjectInfoVo.setStatus("0");
                wbProjectInfoService.updateByBo(BeanUtil.toBean(wbProjectInfoVo, WbProjectInfoBo.class));
            }
        }
        return R.ok();
    }

    /**
     * 删除计划
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:projectInfo:remove")
    @Log(title = "计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(wbProjectInfoService.deleteWithValidByIds(List.of(ids), true));
    }
}
